class MoveWindow {
    constructor($trg) {
        this.$trg = $trg;
        this._width = this.$trg.width();
        this._height = this.$trg.height()
        this._init();
    }

    _init() {
        var that = this;
        this.$trg.on('touchstart', function(e) {

            that._spaceX =  $(this).offset().left - e.touches[0].clientX;
            that._spaceY = $(this).offset().top - e.touches[0].clientY;
            $('body').on('touchmove', function (event) {
                event.preventDefault();
            });
        });

        this.$trg.on('touchmove', function(e) {
            e.touches[0].clientY - that._startY;
            var l = e.touches[0].clientX + that._spaceX;
            var t = e.touches[0].clientY + that._spaceY;
            if(l < 0) {
                l = 0;
            }
            if((l + that._width) > $(window).width()) {
                l = $(window).width() - that._width
            }

            if(t < 0) {
                t = 0;
            }

            if((t + that._height) > $(window).height()) {
                t = $(window).height() - that._height
            }
            $(this).css({
                left : l,
                top : t,
            })
        });

        this.$trg.on('touchend', function(e) {
            $('body').off('touchmove');
            var {left, top} = $(this).offset();
            var leftFinal = left + that._width/2;
            var topFinal = top + that._height/2;
            var centerW = $(window).width()/2;
            var centerH = $(window).height()/2
            console.log(leftFinal, topFinal);
            if(centerW > leftFinal) {
                //左边
                if(centerH > topFinal) {
                    //左上
                    if(leftFinal > topFinal) {
                        //靠上
                        $(this).css({
                            top : 0,
                        })
                    }else {
                        //靠左
                        $(this).css({
                            left : 0,
                        })
                    }
                }else {
                    //左下
                    if(leftFinal > ($(window).height() - topFinal)) {
                        //靠下
                        $(this).css({
                            top : $(window).height() - that._height
                        })
                    }else {
                        //靠左
                        $(this).css({
                            left : 0,
                        })
                    }
                }
            }else {
                //右边
                 if(centerH > topFinal) {
                    //右边上
                    if(($(window).width() - leftFinal) > topFinal) {
                        //靠上
                        $(this).css({
                            top : 0,
                        })
                    }else {
                        //靠右
                        $(this).css({
                            left : $(window).width() - that._width,
                        })
                    }
                }else {
                    //右边下
                    if(($(window).height() - topFinal) < ($(window).width() - leftFinal)) {
                        console.log('xia')
                        //靠下
                        $(this).css({
                            top : $(window).height() - that._height,
                        })
                    }else {
                        //靠右
                        $(this).css({
                            left : $(window).width() - that._width,
                        })
                    }
                }
            }
        });
    }
}

export default MoveWindow